package com.huahang.fashionpress.controller;


import com.huahang.fashionpress.form.FormOrder;
import com.huahang.fashionpress.form.FormProduct;
import com.huahang.fashionpress.service.impl.AddressServiceImpl;
import com.huahang.fashionpress.service.impl.CartServiceImpl;
import com.huahang.fashionpress.service.impl.OrderServiceImpl;
import com.huahang.fashionpress.vo.ProductClientVO;
import com.mysql.cj.protocol.x.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping("/shopcar")
public class ShopcarController {
    @Autowired
    private CartServiceImpl cartService;
    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private AddressServiceImpl addressService;

    @Autowired
    WebSocket webSocket;
    //跳转购物车页面，并显示所有购物车商品
    @RequestMapping("/")
    public ModelAndView getShopcarView(Map<String,Object> map, HttpSession session)
    {
        Integer userID = (Integer) session.getAttribute("userID");
        List<ProductClientVO> cartList = cartService.getCarList(userID,0,100);
//        if(cartList == null || cartList.isEmpty()){
//            System.out.println("购物车为空");
//        }
        //System.out.println(cartList.get(0).getProductQuantity());
        map.put("cartList",cartList);
        return new ModelAndView("client/shopcar", map);
    }

    /**
     *  修改购物车商品数量
     * @param productdate
     * @param userID
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST, headers = "Accept=application/json", produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String shopcarUpdate(@RequestBody String productdate, HttpSession session){
//        System.out.println("shopcarUpdate");
//        System.out.println(productdate);
        Integer userID = (Integer) session.getAttribute("userID");
        List strArray = Arrays.asList(productdate.split("\""));
        String quantity= (String) strArray.get(3);
        String productID = (String) strArray.get(7);
//        System.out.println(productID);
//        System.out.println(quantity);
        int productQuantity = Integer.parseInt(quantity);
        String data = null;
        if(cartService.updateProductQuantity(productID,userID,productQuantity))
        {
            data = "yes";
        }
        else
        {
            data = "no";
        }
        return data;
    }


    /**
     * 移除购物车商品的方法
     * @param productId
     * @param userID
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST, headers = "Accept=application/json", produces="text/html;charset=UTF-8;")
    @ResponseBody
    public String shopcarDelete(@RequestBody String productId,HttpSession session){
//        System.out.println("shopcarDelete");
//        System.out.println(productId);
        Integer userID = (Integer) session.getAttribute("userID");
        List strArray = Arrays.asList(productId.split("\""));
        String productID = (String) strArray.get(3);
        String data = null;
//        System.out.println(productID);
        if(cartService.deleteSingleProductInCar(productID,userID))
        {
            data = "yes";
        }
        else
        {
            data = "no";
        }
        return data;
    }

    /**
     * 将商品添加到购物车的方法
     */
    @RequestMapping(value="/addCart")
    public ModelAndView addCart(String productID, HttpSession session) {
        Integer userID = (Integer) session.getAttribute("userID");
        int productQuantity = 1;
        //获取系统时间
        Date date = new Date();
        //创建商品
        cartService.addProductIntoCart(productID, userID, new java.sql.Date(date.getTime()), productQuantity);
        return new ModelAndView("redirect:/shopcar/");
    }

    @RequestMapping(value="/sumMoney", method=RequestMethod.POST)
    @ResponseBody
    public String sumMoney(String list , Map<String,Object> map, HttpSession session) {
        List<String> productIdList = Arrays.asList(list.split(","));
        List<FormProduct> formProductList = new ArrayList<FormProduct>();
        Integer userID = (Integer) session.getAttribute("userID");
        BigDecimal total = new BigDecimal(0);
        //创建商品
        for (String productId: productIdList) {
            ProductClientVO product = cartService.getProductQuantity(userID,productId);
            FormProduct formProductproduct = new FormProduct();
            formProductproduct.setProductId(productId);
            formProductproduct.setProductPrice(product.getProductPrice());
            total.add(formProductproduct.getProductPrice());
            formProductproduct.setProductQuantity(product.getProductQuantity());
            formProductList.add(formProductproduct);
        }

        //获取用户默认地址
        Integer addressID = addressService.getAllAddress(userID).get(0).getAddressId();

        //创建订单
        FormOrder formOrder = new FormOrder();
        formOrder.setAddressID(addressID);
        formOrder.setOrderPrice(total);
        formOrder.setUserID(userID);

        String orderId = orderService.addSingleOrder(formOrder, formProductList);
        webSocket.sendMessage("您有新的订单 订单编号是"+orderId);

        return "success";
    }

}
